package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.dto.AccountLoginDTO;
import cn.ronghuanet.auth.dto.LoginDTO;
import cn.ronghuanet.auth.service.ILoginService;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.BaseLoginMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService service;

    @PostMapping
    public AjaxResult login(@RequestBody LoginDTO loginDTO){
        try {
            // 返回给页面 token 和 一个 emp数据
//            {"token":"szkjdfgjsaduyhgv","user":"{}"}
            Map<String,Object> map = service.login(loginDTO);
            return AjaxResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PutMapping("/out")
    public AjaxResult loginOut(HttpServletRequest request){
        try {
            // 获取token
            String token = request.getHeader("token");
            // 除以map中的对应数据
            BaseLoginMap.loginMap.remove(token);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody AccountLoginDTO dto){
        Map<String, Object> map = service.accountLogin(dto);
        return AjaxResult.success(map);
    }

    /**
     * 微信登录
     * @return
     */
    @PostMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")String code){
        Map<String, Object> map = service.wechatLogin(code);
        return AjaxResult.success(map);
    }
}
